﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Autodesk.Revit.DB;
using YJKRoomInspection.FindRoomMethod;
using YJKRoomInspection.Model;
using YJKRoomInspection.Properties;
using YJKRoomInspection.Utiltiy;
using YJKInspectModelFunc.ModelLayer;

namespace YJKRoomInspection.SpecialUse
{
	public class FrontRoomFind_CmdFrontRoomFind : IFrontRoomFind<FrontRoomPacker>, IPackerFind<FrontRoomPacker>, IBase<FrontRoomPacker>
	{
		List<FrontRoomPacker> IPackerFind<FrontRoomPacker>.FindPackers(Autodesk.Revit.DB.Document inputDocumnt, params object[] others)
		{
			this.m_doc = inputDocumnt;
			List<Phase> list = new List<Phase>();
			PhaseArray phases = inputDocumnt.Phases;
			List<Level> useLevels = others[0] as List<Level>;
			List<int> list2 = new List<int>();
			for (int i = 0; i < phases.Size; i++)
			{
				list.Add(phases.get_Item(i));
			}
			this.m_FrontRoomByElement = FindFrontRoom_ElementMethod.FindAllRoomPackers(inputDocumnt, useLevels, list, out this.m_AllPacker);
			for (int j = 0; j < this.m_AllPacker.Count; j++)
			{
				if (this.IsFrontRoomByName(this.m_AllPacker[j]))
				{
					this.m_AllPacker[j].bIsNameMatch = true;
					this.m_AllFrontRoom.Add(this.m_AllPacker[j]);
					list2.Add(this.m_AllPacker[j].ThisRoom.Id.IntegerValue);
				}
			}
			for (int k = 0; k < this.m_FrontRoomByElement.Count; k++)
			{
				if (!list2.Contains(this.m_FrontRoomByElement[k].ThisRoom.Id.IntegerValue))
				{
					FrontRoomPacker frontRoomPacker = this.JudgeIsFrontRoomByEle(this.m_FrontRoomByElement[k]);
					if (frontRoomPacker != null)
					{
						frontRoomPacker.IsFrontRoom = true;
						frontRoomPacker.bIsNameMatch = false;
						frontRoomPacker = this.FindXTDoorAndAddData(frontRoomPacker);
						this.m_AllFrontRoom.Add(frontRoomPacker);
					}
				}
			}
			this.m_AllFrontRoom = this.FilterSameFrontRoom(this.m_AllFrontRoom);
			return this.m_AllFrontRoom;
		}

		private List<FrontRoomPacker> FilterSameFrontRoom(List<FrontRoomPacker> m_AllFrontRoom)
		{
			List<FrontRoomPacker> list = new List<FrontRoomPacker>();
			List<int> list2 = new List<int>();
			foreach (FrontRoomPacker frontRoomPacker in m_AllFrontRoom)
			{
				list2.Add(frontRoomPacker.ThisRoom.Id.IntegerValue);
			}
			list2 = list2.Distinct<int>().ToList<int>();
			for (int i = 0; i < m_AllFrontRoom.Count; i++)
			{
				for (int j = 0; j < list2.Count; j++)
				{
					if (m_AllFrontRoom[i].ThisRoom.Id.IntegerValue == list2[j])
					{
						list2.RemoveAt(j);
						list.Add(m_AllFrontRoom[i]);
						j--;
					}
				}
			}
			return list;
		}

		private List<FrontRoomDoorPacker> FilterOutWall(FrontRoomPacker roomPackers)
		{
			List<FrontRoomDoorPacker> list = new List<FrontRoomDoorPacker>();
			for (int i = 0; i < roomPackers.thisFrontRoomDoor.Count; i++)
			{
				list.Add(roomPackers.thisFrontRoomDoor[i]);
			}
			for (int j = 0; j < list.Count; j++)
			{
				if (list[j].LstConctRooms.Count == 1)
				{
					int count = list.Count;
					list.Remove(list[j]);
					if (list.Count != count - 1)
					{
						throw new ArgumentException();
					}
				}
			}
			return list;
		}

		private List<FrontRoomDoorPacker> FilterEauipmentDoor(FrontRoomPacker roomPackers, List<FrontRoomDoorPacker> m_RoomPackers)
		{
			List<FrontRoomPacker> list = new List<FrontRoomPacker>();
			list = roomPackers.NearFrontRooms;
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i].LstBaseDoorPacker.Count == 1 && list[i].LstHostElementPacker.Count == 0)
				{
					m_RoomPackers.Remove(list[i].thisFrontRoomDoor[0]);
				}
			}
			return m_RoomPackers;
		}

		private List<FrontRoomDoorPacker> FindStairDoorNum(FrontRoomPacker roomPackers, List<FrontRoomDoorPacker> m_RoomPackers)
		{
			List<FrontRoomPacker> list = new List<FrontRoomPacker>();
			list = roomPackers.NearFrontRooms;
			int num = 0;
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i].LstBaseDoorPacker.Count >= 1 && list[i].LstHostElementPacker.Count >= 1 && Utility.JudgeStairRoomByArea(this.m_doc, list[i]))
				{
					foreach (FrontRoomDoorPacker item in list[i].thisFrontRoomDoor)
					{
						if (m_RoomPackers.Contains(item))
						{
							num++;
							m_RoomPackers.Remove(list[i].thisFrontRoomDoor[0]);
						}
					}
				}
			}
			roomPackers.StairfDoorNum = num;
			return m_RoomPackers;
		}

		private FrontRoomPacker JudgeIsFrontRoomByEle(FrontRoomPacker tmpPacker)
		{
			List<FrontRoomDoorPacker> list = new List<FrontRoomDoorPacker>();
			bool flag = false;
			int num = 0;
			list = this.FilterOutWall(tmpPacker);
			this.FilterEauipmentDoor(tmpPacker, list);
			list = this.FindStairDoorNum(tmpPacker, list);
			foreach (FrontRoomDoorPacker frontRoomDoorPacker in list)
			{
				if (Regex.IsMatch(frontRoomDoorPacker.FamilySymboleName, Resources.Str_FireDoorNameOne))
				{
					flag = true;
					num++;
				}
				else if (Regex.IsMatch(frontRoomDoorPacker.FamilySymboleName, Resources.Str_FireDoorNameTwo))
				{
					flag = true;
					num++;
				}
				else
				{
					if (!Regex.IsMatch(frontRoomDoorPacker.FamilySymboleName, Resources.Str_FireDoorNameThree))
					{
						flag = false;
						break;
					}
					flag = true;
					num++;
				}
			}
			if (flag)
			{
				tmpPacker.FireProofDoorNum = -1;
				return tmpPacker;
			}
			return null;
		}

		private FrontRoomPacker FindXTDoorAndAddData(FrontRoomPacker tmpRoomPacker)
		{
			int num = 0;
			if (tmpRoomPacker.ThisRoom.Name.Contains("XT"))
			{
				tmpRoomPacker.thisDoorNum.FireLadderDoor = tmpRoomPacker.thisFrontRoomDoor.Count;
				if (tmpRoomPacker.LstHostElementPacker.Count > 1)
				{
					tmpRoomPacker.thisDoorNum.StairDoor = tmpRoomPacker.thisFrontRoomDoor.Count;
				}
				tmpRoomPacker.thisDoorNum.FireProogDoor = tmpRoomPacker.FireProofDoorNum;
			}
			else if (tmpRoomPacker.ThisRoom.Name.Contains("消") && tmpRoomPacker.ThisRoom.Name.Contains("梯"))
			{
				tmpRoomPacker.thisDoorNum.FireLadderDoor = tmpRoomPacker.thisFrontRoomDoor.Count;
				if (tmpRoomPacker.LstHostElementPacker.Count > 1)
				{
					tmpRoomPacker.thisDoorNum.StairDoor = tmpRoomPacker.thisFrontRoomDoor.Count;
				}
				tmpRoomPacker.thisDoorNum.FireProogDoor = tmpRoomPacker.FireProofDoorNum;
			}
			foreach (FrontRoomDoorPacker frontRoomDoorPacker in tmpRoomPacker.thisFrontRoomDoor)
			{
				if (frontRoomDoorPacker.FamilyName.Contains("消") && frontRoomDoorPacker.FamilyName.Contains("梯"))
				{
					num++;
				}
			}
			tmpRoomPacker.thisDoorNum.FireLadderDoor = num;
			tmpRoomPacker.thisDoorNum.StairDoor = tmpRoomPacker.StairfDoorNum;
			tmpRoomPacker.thisDoorNum.FireProogDoor = tmpRoomPacker.FireProofDoorNum;
			return tmpRoomPacker;
		}

		private bool IsFrontRoomByName(FrontRoomPacker roomPacker)
		{
			string text = Utility.SubstringRoomName(roomPacker.ThisRoom.Name);
			for (int i = 0; i < StaticRescource.FrontRoomTypes.Count<string>(); i++)
			{
				if (text.Contains(StaticRescource.FrontRoomTypes[i]))
				{
					roomPacker.IsFrontRoom = true;
					roomPacker.thisAreaOrSide.FrontRoomType = text;
					return true;
				}
			}
			return false;
		}

		private List<FrontRoomPacker> m_AllFrontRoom = new List<FrontRoomPacker>();

		private List<FrontRoomPacker> m_AllPacker = new List<FrontRoomPacker>();

		private List<FrontRoomPacker> m_FrontRoomByElement = new List<FrontRoomPacker>();

		private Document m_doc;
	}
}
